import { message } from 'antd';
export default {
  namespace: 'fccc',

  state: {
    light1Params: [], // 灯管1的信息
    light2Params: [], // 灯管2的信息
    light3Params: [], // 灯管3的信息
    light4Params: [], // 灯管4的信息
    cutterImageModalVisibleBig: false, // 铡刀的图片展示是否显示
    frontCutterCamSrc: null, // 铡刀那里标定的时候图片展示的内容
    hintInfo: '无消息', // 铡刀标定的时候的提示消息
  },

  // 和后台交互
  effects: {
    *handleCutterCameraCalMsg({ payload }, { call, put }) {
      const { msg } = payload;
      if (msg.val === 'updateLightParamsBig') {
        //更新界面上显示的参数列表
        yield put({
          type: 'getLightParams',
        });
      } else if (msg.val == 'updateCutterCalImageBig') {
        // 更新显示的图片内容
        yield put({
          type: 'updateCutterCalImageBig',
          payload: [true, msg.cutterCalImage, msg.hintInfo],
        });
      }
    },
    *getLightParams({ payload }, { call, put }) {
      const lightParams = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_get_light_params_big',
        []
      );
      yield put({
        type: 'updateLightParamsBig',
        payload: lightParams,
      });
    },
    *checkLightPos({ payload }, { call, put }) {
      const lightParams = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_check_light_pos',
        [payload]
      );
    },
    *saveLightParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_save_light_params',
        [payload]
      );
      if (response !== 'ok') {
        message.error('保存灯管参数失败,原因:' + response, 5);
      } else {
        message.success('保存灯管参数成功!', 3);
      }
      yield put({
        type: 'getLightParams',
      });
    },
    *calCutterCam1({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_cal_cutter_cam1',
        [payload]
      );
      if (response !== 'ok') {
        message.error('1号相机标定失败: ' + response, 5);
      }
      yield put({
        type: 'getLightParams',
      });
    },

    *calCutterCam2({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_cal_cutter_cam2',
        [payload]
      );
      if (response !== 'ok') {
        message.error('2号相机标定失败: ' + response, 5);
      }
      yield put({
        type: 'getLightParams',
      });
    },

    *verifyCutterCam1Cal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_verify_cutter_cam1_cal',
        []
      );
      if (response !== 'ok') {
        message.error('1号相机验证失败: ' + response, 5);
      }
    },
    *verifyCutterCam2Cal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_verify_cutter_cam2_cal',
        []
      );
      if (response !== 'ok') {
        message.error('2号相机验证失败: ' + response, 5);
      }
    },
    *verifyCutterCamTotal({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_verify_cutter_cam_total',
        []
      );
      if (response !== 'ok') {
        message.error('总板长验证失败: ' + response, 5);
      }
    },

    *openCutterCamCalDir({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_open_cutter_cam_cal_dir',
        []
      );
      if (response !== 'ok') {
        message.error('目录打开失败: ' + response, 5);
      } else {
        message.success('目录打开成功!', 3);
      }
    },

    *cutterDown({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_cutter_down',
        [payload]
      );
      if (response !== 'ok') {
        message.error('铡刀下降指令发送失败: ' + response, 5);
      } else {
        message.success('铡刀下降指令发送成功!', 3);
      }
    },

    *deleteTotalCutterCalImage({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_delete_total_cutter_cal_image',
        []
      );
      if (response !== 'ok') {
        message.error('删除失败: ' + response, 5);
      } else {
        message.success('删除成功!', 3);
      }
    },

    *verifyCutterBeginCheck({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'bigCutterCamCal',
        'web_request_verify_cutter_begin_check_big',
        []
      );
      if (response !== 'ok') {
        message.error('验证失败: ' + response, 5);
      } else {
      }
    },
    *closeCutDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'bigCutterZmc', 'close_door_alarm');
      if (response === 0) {
        message.success('关闭铡刀门报警成功', 3);
      } else {
        message.error('关闭铡刀门报警失败!' + response, 5);
      }
    },
    *openCutDoorAlarm({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'bigCutterZmc', 'open_door_alarm');
      if (response === 0) {
        message.success('打开铡刀门报警成功', 3);
      } else {
        message.error('打开铡刀门报警失败!' + response, 5);
      }
    },
    *setBigCutterOPStart({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'bigCutterCamCal', 'web_request_set_big_cutter_OP_start', [payload]);
      if (response !== "ok") {
        message.error(response, 3);
      }else {
        message.success("成功!", 3)
      }
    },
  },

  // 状态更新
  reducers: {
    updateLightParamsBig(state, action) {
      return {
        ...state,
        light1Params: action.payload[0],
        light2Params: action.payload[1],
        light3Params: action.payload[2],
        light4Params: action.payload[3],
      };
    },

    closeCutterCalImageModalBig(state, action) {
      return {
        ...state,
        cutterImageModalVisibleBig: false,
        frontCutterCamSrc: null,
        hintInfo: '无提示消息',
      };
    },

    updateCutterCalImageBig(state, action) {
      let imageShow;
      if (action.payload[1] != null) {
        imageShow = 'data:image/jpeg;base64,' + action.payload[1];
      } else {
        imageShow = null;
      }
      return {
        ...state,
        cutterImageModalVisibleBig: action.payload[0],
        frontCutterCamSrc: imageShow,
        hintInfo: action.payload[2],
      };
    },
  },
};
